<?php

namespace Cps\Controller;

use Think\Controller;
use Think\Page;

class IndexController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");
        if (!isset($_SESSION['cpsinfo']) || $_SESSION['cpsinfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {
        if ($_SESSION['cpsinfo']['pid'] == 0) {
            $cpsModel = new \Cps\Model\CpsmemberModel();
            $list = $cpsModel->where("pid='%d'", $_SESSION['cpsinfo']['id'])->select();
            for ($i = 0; $i < count($list); $i++) {
                $temp[] = explode(',', $list[$i]['refer']);
            }

            $Model = new \Think\Model();
            for ($i = 0; $i < count($temp); $i++) {
                for ($j = 0; $j < count($temp[$i]); $j++) {
                    $sql = "SELECT tgi.id,tgi.refname,tgi.refurl,tgi.shorturl,tgi.game_id,tgi.area_num,g.gamename,a.areaname FROM tg_game_info AS tgi ,game_list AS g ,game_server_list AS a 
                    WHERE tgi.id = " . $temp[$i][$j] . " AND tgi.game_id = g.id AND tgi.game_id = a.game_id AND tgi.area_num = a.area_num";
                    $gameinfo = $Model->query($sql);
                    if (strpos($list[$i]['refer'], $gameinfo[0]['id']) !== FALSE) {
                        $list[$i]['game'].=$gameinfo[0]['gamename'] . '-' . $gameinfo[0]['areaname'] . '|';
                    }
                }
            }
//            for($i=0;$i<count($list);$i++){
//                for($j=0;$j<count($gameinfo);$j++){
//                    if($list[$i][''])
//                }
//            }
            $this->assign('list', $list);
            $this->display();
        } else {
            $cps = new \Cps\Model\CpsModel();
            $count = $cps->getGameList(); //没有参数获得的是总条数

            $Page = new Page($count, 15); // 实例化分页类 传入总记录数
            $show = $Page->show(); // 分页显示输出
            $list = $cps->getGameList(1, $Page->firstRow, $Page->listRows);

            $tg_game_info = M('tg_game_info');
            $tginfo = $tg_game_info->where("refname='%s'", $_SESSION['cpsinfo']['username'])->order('id asc')->select();

            if (isset($tginfo)) {
                for ($i = 0; $i < count($list); $i++) {
                    for ($j = 0; $j < count($tginfo); $j++) {
                        if ($list[$i]['game_id'] == $tginfo[$j]['game_id'] && $list[$i]['area_num'] == $tginfo[$j]['area_num']) {
                            $list[$i]['url'] = $tginfo[$j]['refurl'];
                            $list[$i]['surl'] = $tginfo[$j]['shorturl'];
                            $list[$i]['refer'] = $tginfo[$j]['id'];
                        }
                    }
                }
            }
            
            //获得管理账号
            $cpsModel=M("cps_member");
            $cpsM=$cpsModel->where("id='%d'",$_SESSION['cpsinfo']['pid'])->find();
                        
            //var_dump($list);exit;
            $this->assign('cpsM',$cpsM);
            $this->assign('list', $list); // 赋值数据集
            $this->assign('page', $show); // 赋值分页输出
            $this->display('cindex');
        }
    }

}
